home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
8_11.lha
/
8_11
/
rc.h
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
3KB
|
135 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ Exercise 8.11
/ Generic random access to objects kept within a file.
/ The user would use the generic class randomOBJ
/ for a user type TYPE by the following steps:
/
/ declare(randomOBJ,TYPE)
/ implement(randomOBJ,TYPE)
/ ...
/ randomOBJ(TYPE) x("filename",
/ read-err-func, write-err-func);
/ randomOBJ(TYPE) y("filename");
ifndef RANDACCESS_H
define RANDACCESS_H
include <stdio.h>
include <generic.h>
include "8_11a.h" /* EXPAND */
*
An extended FILE which keeps track of its
current location within the file. This class
has no type-specific implementation and can
be used across all instantiations of randomOBJ.
/
lass eFILE
long fileoffset;
FILE *fp;
eFILE() { };
ublic:
friend eFILE *efopen(char *file);
friend void efclose(eFILE *efp);
friend int egetobj(eFILE *efp, long offset,
void *obj, unsigned szobj);
friend int eputobj(eFILE *efp, long offset,
void *obj, unsigned szobj);
;
define randomOBJdeclare(type) \
class randomOBJ(type); \
\
class OBJrandomloc(type) \
{ \
randomOBJ(type) *rOBJ; \
long thisoffset; \
\
public: \
OBJrandomloc(type)(randomOBJ(type) *robj, \
long offset) \
{ \
rOBJ = robj; \
thisoffset = offset; \
} \
\
operator type(); \
\
OBJrandomloc(type) operator=(type newobj); \
OBJrandomloc(type) operator=( \
OBJrandomloc(type) r); \
}; \
\
class randomOBJ(type) \
{ \
eFILE *rfp; \
void (*rdrfnc)(type *); \
void (*wtrfnc)(type *); \
friend OBJrandomloc(type); \
OBJrandomloc(type) operator=(type newobj); \
\
public: \
randomOBJ(type)(char *filename, \
void (*rfnc)(type *) = 0, \
void (*wfnc)(type *) = 0) \
{ \
rfp = efopen(filename); \
rdrfnc = rfnc; \
wtrfnc = wfnc; \
} \
\
~randomOBJ(type)() \
{ \
efclose(rfp); \
} \
\
int openedokay() \
{ \
return rfp != 0; \
} \
\
OBJrandomloc(type) operator[](long offset) \
{ \
OBJrandomloc(type) ret(this, offset); \
return ret; \
} \
};
define randomOBJimplement(type) \
OBJrandomloc(type)::operator type() \
{ \
type x; \
if (!egetobj(rOBJ->rfp, thisoffset, &x, \
sizeof x)) \
if (rOBJ->rdrfnc) \
(*rOBJ->rdrfnc)(&x); \
return x; \
} \
\
OBJrandomloc(type) OBJrandomloc(type)::operator=( \
type newobj) \
{ \
if (!eputobj(rOBJ->rfp, thisoffset, \
&newobj, sizeof newobj)) \
if (rOBJ->wtrfnc) \
(*rOBJ->wtrfnc)(&newobj); \
return *this; \
} \
\
OBJrandomloc(type) OBJrandomloc(type)::operator=( \
OBJrandomloc(type) r) \
{ \
return (*this = (type) r); \
}
endif /* RANDACCESS_H */